package com.lmaos.basetools.base;

import java.math.BigDecimal;
import java.math.RoundingMode;

/** 简单数据工具
 * 
 * @author lmaos
 *
 * <p>作者：张星宇</p>
 * <p>时间：2017年5月26日 下午3:51:43</p>
 * <p>说明:数字转换工具	,对象转数字类型,数字转bytes	</p>
 *
 */
public class SNumberUtils {
	public static byte toByte(Object value){
		return toByte(value, (byte)0);
	}
	public static byte toByte(Object value,byte def){
		
		if(value == null){
			return def;
		}
		if(value instanceof Number){
			return ((Number) value).byteValue();
		}
		try{
			return (byte)Integer.parseInt(String.valueOf(value));
		}catch(Exception e){
			return def;
		}
	}
	public static byte[] toBytes(Object[] objs,byte def){
		byte[] vs = new byte[objs.length];
		for (int i = 0; i < vs.length; i++) {
			vs[i] = toByte(objs[i], def);
		}
		return vs;
	}
	public static short toShort(Object value){
		return toShort(value,(short)0);
	}
	public static short toShort(Object value,short def){
		
		if(value == null){
			return def;
		}
		if(value instanceof Number){
			return ((Number) value).shortValue();
		}
		try{
			return Short.parseShort(String.valueOf(value));
		}catch(Exception e){
			return def;
		}
	}
	public static short[] toShorts(Object[] objs,short def){
		short[] vs = new short[objs.length];
		for (int i = 0; i < vs.length; i++) {
			vs[i] = toShort(objs[i], def);
		}
		return vs;
	}
	public static int toInt(Object value){
		return toInt(value, 0);
	}
	public static int toInt(Object value,int def){
		
		if(value == null){
			return def;
		}
		if(value instanceof Number){
			return ((Number) value).intValue();
		}
		try{
			return Integer.parseInt(String.valueOf(value));
		}catch(Exception e){
			return def;
		}
	}
	public static int[] toInts(Object[] objs,int def){
		int[] vs = new int[objs.length];
		for (int i = 0; i < vs.length; i++) {
			vs[i] = toInt(objs[i], def);
		}
		return vs;
	}
	public static long toLong(Object value){
		return toLong(value, 0);
	}
	public static long toLong(Object value,long def){
		
		if(value == null){
			return def;
		}
		if(value instanceof Number){
			return ((Number) value).longValue();
		}
		try{
			return Long.parseLong(String.valueOf(value));
		}catch(Exception e){
			return def;
		}
	}
	public static long[] toLongs(Object[] objs,long def){
		long[] vs = new long[objs.length];
		for (int i = 0; i < vs.length; i++) {
			vs[i] = toLong(objs[i], def);
		}
		return vs;
	}
	public static float toFloat(Object value){
		return toFloat(value, 0);
	}
	public static float toFloat(Object value,float def){
		
		if(value == null){
			return def;
		}
		if(value instanceof Number){
			return ((Number) value).floatValue();
		}
		try{
			return Float.parseFloat(String.valueOf(value));
		}catch(Exception e){
			return def;
		}
	}
	public static float[] toFloats(Object[] objs,float def){
		float[] vs = new float[objs.length];
		for (int i = 0; i < vs.length; i++) {
			vs[i] = toFloat(objs[i], def);
		}
		return vs;
	}
	public static double toDouble(Object value){
		return toDouble(value, 0);
	}
	public static double toDouble(Object value,double def){
		
		if(value == null){
			return def;
		}
		if(value instanceof Number){
			return ((Number) value).doubleValue();
		}
		try{
			return Double.parseDouble(String.valueOf(value));
		}catch(Exception e){
			return def;
		}
	}
	public static double[] toDoubles(Object[] objs,double def){
		double[] vs = new double[objs.length];
		for (int i = 0; i < vs.length; i++) {
			vs[i] = toDouble(objs[i], def);
		}
		return vs;
	}
	public static boolean toBoolean(Object value){
		return Boolean.parseBoolean(String.valueOf(value));
	}
	public static boolean[] toBooleans(Object[] objs){
		boolean[] vs = new boolean[objs.length];
		for (int i = 0; i < vs.length; i++) {
			vs[i] = toBoolean(objs[i]);
		}
		return vs;
	}
	public static BigDecimal toBigDecimal(Object value){
		return toBigDecimal(value, new BigDecimal(0));
	}
	public static BigDecimal toBigDecimal(Object value,BigDecimal def){
		if(value == null){
			return def;
		}
		if(value instanceof BigDecimal){
			return (BigDecimal) value;
		}
		if(value instanceof Number){
			return new BigDecimal(((Number)value).doubleValue());
		}
		try {
			return new BigDecimal(String.valueOf(value));
		} catch (Exception e) {
			return def;
		}
	}
	public static byte[] toBytesByShortB(short val){
		byte[] bs = new byte[2];
		bs[0] = (byte) ((val>>8)&0xFF);
		bs[1] = (byte) ((val)&0xFF);
		return bs;
	}
	public static short toShortByBytesB(byte[] bs){
		short val = (short) (((bs[0]&0xFF)<<8)|(bs[1]&0xFF));
		return val;
	}
	public static byte[] toBytesByShortL(short val){
		byte[] bs = new byte[2];
		bs[1] = (byte) ((val>>8)&0xFF);
		bs[0] = (byte) ((val)&0xFF);
		return bs;
	}
	public static short toShortByBytesL(byte[] bs){
		short val = (short) (((bs[1]&0xFF)<<8)|(bs[0]&0xFF));
		return val;
	}
	
	// int 
	public static byte[] toBytesByIntB(int val){
		byte[] bs = new byte[4];
		bs[0] = (byte) ((val>>24)&0xFF);
		bs[1] = (byte) ((val>>16)&0xFF);
		bs[2] = (byte) ((val>>8)&0xFF);
		bs[3] = (byte) ((val)&0xFF);
		return bs;
	}
	public static int toIntByBytesB(byte[] bs){
		int val = (int) (((bs[0]&0xFF)<<24)
				|((bs[1]&0xFF)<<16)
				|((bs[2]&0xFF)<<8)
				|(bs[3]&0xFF));
		return val;
	}
	
	public static byte[] toBytesByIntL(int val){
		byte[] bs = new byte[4];
		bs[3] = (byte) ((val>>24)&0xFF);
		bs[2] = (byte) ((val>>16)&0xFF);
		bs[1] = (byte) ((val>>8)&0xFF);
		bs[0] = (byte) ((val)&0xFF);
		return bs;
	}
	public static int toIntByBytesL(byte[] bs){
		int val = (int) (((bs[3]&0xFF)<<24)
				|((bs[2]&0xFF)<<16)
				|((bs[1]&0xFF)<<8)
				|(bs[0]&0xFF));
		return val;
	}
	
	// long
	public static byte[] toBytesByLongB(long val){
		byte[] bs = new byte[8];
		bs[0] = (byte) ((val>>56)&0xFF);
		bs[1] = (byte) ((val>>48)&0xFF);
		bs[2] = (byte) ((val>>40)&0xFF);
		bs[3] = (byte) ((val>>32)&0xFF);
		bs[4] = (byte) ((val>>24)&0xFF);
		bs[5] = (byte) ((val>>16)&0xFF);
		bs[6] = (byte) ((val>>8)&0xFF);
		bs[7] = (byte) ((val)&0xFF);
		return bs;
	}
	public static long toLongByBytesB(byte[] bs){
		long val = (long) (
				 ((long)(bs[0]&0xFF)<<56)
				|((long)(bs[1]&0xFF)<<48)
				|((long)(bs[2]&0xFF)<<40)
				|((long)(bs[3]&0xFF)<<32)
				|((long)(bs[4]&0xFF)<<24)
				|((long)(bs[5]&0xFF)<<16)
				|((long)(bs[6]&0xFF)<<8)
				|(long)(bs[7]&0xFF));
		return val;
	}
	
	public static byte[] toBytesByLongL(long val){
		byte[] bs = new byte[8];
		bs[7] = (byte) ((val>>56)&0xFF);
		bs[6] = (byte) ((val>>48)&0xFF);
		bs[5] = (byte) ((val>>40)&0xFF);
		bs[4] = (byte) ((val>>32)&0xFF);
		bs[3] = (byte) ((val>>24)&0xFF);
		bs[2] = (byte) ((val>>16)&0xFF);
		bs[1] = (byte) ((val>>8)&0xFF);
		bs[0] = (byte) ((val)&0xFF);
		return bs;
	}
	public static long toLongByBytesL(byte[] bs){
		long val = (long) (
				 ((long)(bs[7]&0xFF)<<56)
				|((long)(bs[6]&0xFF)<<48)
				|((long)(bs[5]&0xFF)<<40)
				|((long)(bs[4]&0xFF)<<32)
				|((long)(bs[3]&0xFF)<<24)
				|((long)(bs[2]&0xFF)<<16)
				|((long)(bs[1]&0xFF)<<8)
				|(long)(bs[0]&0xFF));
		return val;
	}
	
	// double
	public static byte[] toBytesByDoubleB(double val){
		
		return toBytesByLongB(Double.doubleToLongBits(val));
	}
	public static double toDoubleByBytesB(byte[] bs){
		long val = toLongByBytesB(bs);
		return Double.longBitsToDouble(val);
	}
	public static byte[] toBytesByDoubleL(double val){
		
		return toBytesByLongL(Double.doubleToLongBits(val));
	}
	public static double toDoubleByBytesL(byte[] bs){
		long val = toLongByBytesL(bs);
		return Double.longBitsToDouble(val);
	}
	
	// float
	public static byte[] toBytesByFloatB(float val){
		
		return toBytesByIntB(Float.floatToIntBits(val));
	}
	public static float toFloatByBytesB(byte[] bs){
		int val = toIntByBytesB(bs);
		return Float.intBitsToFloat(val);
	}
	public static byte[] toBytesByFloatL(float val){
		
		return toBytesByIntB(Float.floatToIntBits(val));
	}
	public static float toFloatByBytesL(byte[] bs){
		int val = toIntByBytesB(bs);
		return Float.intBitsToFloat(val);
	}
	/**
	 * 说明：保留小数点后位数		<br>
	 * 时间：2017年6月1日 下午10:48:08
	 *
	 * @param val
	 * @param px
	 * @return
	 */
	public static double toDoublePx(double val,int px){
		BigDecimal bd = new BigDecimal(val).setScale(px,RoundingMode.HALF_UP);
		return bd.doubleValue();
	}
	public static void main(String[] args) {
		System.out.println(toShortByBytesB(toBytesByShortB((short)32000)));
		System.out.println(toShortByBytesL(toBytesByShortL((short)30000)));
		System.out.println(toIntByBytesB(toBytesByIntB(1234567890)));
		System.out.println(toIntByBytesL(toBytesByIntL(2134567890)));
		System.out.println(toLongByBytesB(toBytesByLongB(34567898765434567L)));
		System.out.println(toLongByBytesL(toBytesByLongL(94567898765434569L)));
		
		System.out.println(toDoubleByBytesB(toBytesByDoubleB(200000.2332323)));
		System.out.println(toDoubleByBytesL(toBytesByDoubleL(9900000.230323)));
		
		System.out.println(toFloatByBytesB(toBytesByFloatB(200000.2332323F)));
		System.out.println(toFloatByBytesL(toBytesByFloatL(9900000.230323F)));
		
		System.out.println(toDoublePx(2000.3242323, 2));
		
	}
	
}
